package com.apps.globals.color.service.impl;

import com.apps.globals.color.model.po.KupaColor;
import com.apps.globals.color.dao.KupaColorMapper;
import com.apps.globals.color.model.vo.HSV;
import com.apps.globals.color.model.vo.RGB;
import com.apps.globals.color.service.IKupaColorService;
import com.apps.globals.color.utils.ColorCountUtils;
import com.apps.sworld.dao.impl.sDaoImpl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 大鹏
 * @since 2020-11-04
 */
@Slf4j
@Service
public class KupaColorServiceImpl extends sDaoImpl<KupaColorMapper, KupaColor> implements IKupaColorService {

    @Override
    public String recognition(BufferedImage bufferedImage, RGB... rgbreject) {
        //提取ppt的主色调
        List<RGB> mainColor= ColorCountUtils.recognition(bufferedImage,rgbreject);
        List<KupaColor> colors=query();
        //颜色名字map
        Map<Integer,String> maps=new HashMap<>();
        for (KupaColor color : colors) {
                for(RGB rgb:mainColor) {
                    double distance = ColorCountUtils.distance(new HSV(rgb), new HSV(new RGB(color.getRgb())));
                    if (distance <= 40) {
                        maps.put(color.getId(), color.getName_ch());
                    }
                }
            }

        String result="";
        // 遍历map中的值组合成中文颜色名称
        for (String value : maps.values())
            result+=value;

        return result;
    }

    @Override
    public RGB findSimilar(RGB rgb) {
        List<KupaColor> colors=query();
        double distance=100;
        RGB result=null;
        for (KupaColor color : colors) {
              RGB now_rgb=new RGB(color.getRgb());
              double now_distance=ColorCountUtils.distance(new HSV(rgb), new HSV(now_rgb));
              if(now_distance < distance) {
                  distance = now_distance;
                  result = now_rgb;
              }
        }
        return result;
    }

    @SneakyThrows
    @Override
    public void onLoad()
    {
//        List<RGB> mainColor= ColorCountUtils.recognition("I:\\素材\\ppt\\节日庆典-1\\节日庆典(51)\\thumb.jpg",new RGB(255,255,255),new RGB(204,204,204));
//        log.info(mainColor.toString());
    }

}
